import sys
from PyQt5.QtWidgets import QWidget,  QLabel, QPushButton, \
    QGridLayout, QApplication, QListWidget, QListWidgetItem, QStackedWidget
from PyQt5.QtGui import QPainter, QPixmap, QImage, QColor, QPen
from PyQt5.QtCore import Qt, QThread, QTimer, pyqtSignal
import cv2
from ChartView import LineChart

from ListWidget import ListWidget
from CalendarWidget import CalendarWidget
#from FramelessWindow import FramelessWindow

StyleSheet = '''
QCalendarWidget QHeaderView {
    qproperty-minimumSectionSize:0;


}
QCalendarWidget QMenu{
    background-color: #2BC0E4;

}
#qt_calendar_calendarview {
    background-color: #2BC0E4;
    font: 16px;

}
#qt_calendar_monthbutton {
    background-color: #2BC0E4;
}
#qt_calendar_yearbutton {
    background-color: #2BC0E4;
}
#qt_calendar_yearedit {
    background-color: rgba(68,84,106);
}
#qt_calendar_navigationbar {
    background-color: #2BC0E4;
}
QToolButton#qt_calendar_prevmonth {
    background: transparent;
    border: none;
    width: 40px;

}
QToolButton#qt_calendar_nextmonth {
    background: transparent;
    border: none;
    width: 40px;

}

QCalendarWidget QAbstractItemView {
    color: gray;
background-color: rgba(68,84,106);

}

QToolBoxButton { 
                background-color: rgb(255, 255, 255, 50);
              min-height: 30px; }
}

QToolBox::tab {

    border: 1px solid #C0C0C0;
    color: black;
}

QToolBox::tab:first {
    color: black;
}

QToolBox::tab:last {  
    color: black;
}

QToolBox::tab:selected { /* italicize selected tabs */
    font: italic;
    color: white;
}

'''

class ClientUI(QWidget):
    closeSignal = pyqtSignal()
    configSignal = pyqtSignal(list)

    def __init__(self, parent=None):
        super(ClientUI, self).__init__(parent)
        self.setMinimumSize(800, 600)
        # 用于显示视频
        self.videoLabel1 = QLabel(self)
        self.videoLabel2 = QLabel(self)
        self.videoLabel1.setMinimumSize(700, 500)
        self.videoLabel2.setMinimumSize(700, 500)
        # 图表
        self.myplot = LineChart()

        self.calendar = CalendarWidget()

        self.ListWidget = ListWidget()

        self.gLayout = QGridLayout()
        self.glayout_init()
        self.plot_init()

        self.connectSolts()
        self.setObjectName("monitorWidget")
        #self.setAttribute(Qt.WA_TranslucentBackground)
        self.setAttribute(Qt.WA_StyledBackground, True)

    def glayout_init(self):
        self.gLayout.addWidget(self.ListWidget, 0, 0, 2, 1, Qt.AlignRight)
        self.gLayout.addWidget(self.myplot, 0, 1, 3, 1)
        self.gLayout.addWidget(self.calendar, 2, 0, Qt.AlignTop)
        self.gLayout.addWidget(self.videoLabel1, 1, 2, Qt.AlignCenter)
        self.gLayout.addWidget(self.videoLabel2, 2, 2)

        self.gLayout.setSpacing(10)
        self.gLayout.setColumnStretch(0, 2)
        self.gLayout.setColumnStretch(1, 3)
        self.gLayout.setColumnStretch(2, 4)
        self.gLayout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.gLayout)

    def connectSolts(self):
        self.calendar.clicked.connect(self.onCalendarClicked)
        self.ListWidget.btnSelected.connect(self.onListBtnClicked)

    def qlabelshow_img(self, label, img):
        a = cv2.resize(img, (label.width(), label.height()))
        dst = cv2.cvtColor(a, cv2.COLOR_BGR2RGBA)
        height, width, channel = dst.shape
        m_image = QImage(dst.data, width, height, QImage.Format_RGB32).rgbSwapped()
        a = QPixmap.fromImage(m_image)
        pix = QPixmap(a)
        label.setPixmap(pix)

    def plot_init(self):
        self.myplot.display()

    def displayVideo(self, img):
        self.qlabelshow_img(self.videoLabel1, img[0])

    def displayHeatMap(self, img):
        self.qlabelshow_img(self.videoLabel2, img)

    def onSetBtnClicked(self):
        self.setDia.show()

    def getSetConfig(self):
        ret = []
        cf, ud, ins = self.setDia.getConfig()
        ret.append(cf)
        ret.append(ud)
        ret.append(ins)
        self.configSignal.emit(ret)

    def setPlotModel(self, model):
        self.myplot.setModel(model)

    def display_plot(self):
        self.myplot.start_display()

    def get_listWidget(self):
        return self.ListWidget

    def getTime(self, date):
        year = date.year()
        month = date.month()
        day = date.day()
        mdate = str(year) + "-" + str(month).rjust(2, "0") + "-" + str(day).rjust(2, "0")
        return mdate

    def onCalendarClicked(self, date):
        armid = self.ListWidget.currentChecked()
        if armid is not None:
            armid = armid.text()
            mdate = self.getTime(date)
            self.configSignal.emit([armid, mdate])

    def onListBtnClicked(self, armid):
        date = self.calendar.selectedDate()
        mdate = self.getTime(date)
        self.configSignal.emit([armid, mdate])

    def currentSelectdDate(self):
        return self.calendar.selectedDate()

    def currentSelectdArm(self):
        return self.ListWidget.currentChecked()

    def updatePlot(self, data):
        self.myplot.update_figure(data)

def main():
     app = QApplication(sys.argv)
     app.setStyleSheet(StyleSheet)

     w = FramelessWindow()
     win = ClientUI(w)

     w.setWidget(win)

     w.show()
     sys.exit(app.exec_())


if __name__ == "__main__":
    main()
